package com.qq2008.game.mojie.server;

import com.qq2008.game.mojie.jdo.Room;
import com.qq2008.game.mojie.service.RoomService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 场景管理器
 */
@Order(10)
@Component
public class SceneServer implements CommandLineRunner {

    @Resource
    private RoomService _roomService;
    // 场景集合
    private static Map<Integer, Room> sceneCache;
    //
    private static RoomService roomService;

    @PostConstruct
    public void init(){
        roomService = _roomService;
    }

    // 获取房间信息
    public static Room getRoom(int mapId) {
        return sceneCache.get(mapId);
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("= 启动场景管理器");
        sceneCache = new HashMap<>();
        System.out.println("λ 正在载入 场景配置 设定...");
        List<Room> mapList = roomService.list(null);
        mapList.forEach((moRoom) -> {
            sceneCache.put(moRoom.getId(), moRoom);
        });
        System.out.println("  场景配置名单 " + sceneCache.size() + " 条");
    }
}
